package com.bootdo.collector.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bootdo.collector.domain.HotWords;
import com.bootdo.collector.domain.PDepart;
import com.bootdo.collector.domain.PSourceUsed;
import com.bootdo.collector.domain.PWarn;
import com.bootdo.collector.utils.ESParse;
import com.bootdo.screen.dao.PssourcelistDao;
import com.bootdo.screen.domain.*;
import com.bootdo.screen.service.*;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

public class PublishCountJob implements Job {
    @Autowired
    JdbcTemplate ThirdJdbcTemplate;

    @Autowired
    PssourcelistService pssourcelistService;

    @Autowired
    private RestClient restClient;

    @Autowired
    private PsareacounttrueService psareacounttrueService;

    @Autowired
    private PssourcenumtrueService pssourcenumtrueService;

    @Autowired
    private PsitemmonitorService psitemmonitorService;

    @Autowired
    private PsuserdataService psuserdataService;

    @Autowired
    private PssitenumtureService pssitenumtureService;

    private final String  PUBESURL ="public_sentiment/doc/_search?size=0";
    private final String  AREACOUNT = "{ \"query\": {\"match\": { \"areaIds\": \"%d\" }} }";
    private final String  SOURCECOUNT = "{ \"query\": {\"term\": { \"cateName\": { \"value\": \"%s\" } }} }";
    private final String SOURCETOPTEN ="{\"aggs\": { \"count\": { \"terms\": { \"field\": \"sourceName\", \"size\": 10 } } }}";


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<PSourceUsed> pss = ThirdJdbcTemplate.query("select id,source_name,level_id from t_source_used", new BeanPropertyRowMapper<>(PSourceUsed.class));
        for (PSourceUsed ps:pss) {
            if (ps.getSource_name().equals("A")) {

            } else {
                PssourcelistDO pssdo = new PssourcelistDO();
                pssdo.setId(ps.getId());
                if (ps.getLevel_id()==2) {
                    pssdo.setScore(30);
                } else {
                    pssdo.setScore(60);
                }
                pssdo.setSource(ps.getSource_name());
                pssourcelistService.saveUpdate(pssdo);
            }
        }

        String res;
        // 华东 华南 华中 华北 西北 西南 东北
        for (int i=1;i<8;i++) {
            res = ESParse.execQuery(restClient, String.format(AREACOUNT, i), PUBESURL);
            PsareacounttrueDO psarea = psareacounttrueService.get(i);
            psarea.setNum(ESParse.getTotal2(res));
            psareacounttrueService.update(psarea);
        }

        PssourcenumtrueDO pssourcenumtrue;
         res = ESParse.execQuery(restClient, String.format(SOURCECOUNT, "news"), PUBESURL);
        pssourcenumtrue = pssourcenumtrueService.get(1);
        pssourcenumtrue.setNum(ESParse.getTotal2(res));
        pssourcenumtrueService.update(pssourcenumtrue);

        res = ESParse.execQuery(restClient, String.format(SOURCECOUNT, "blog"), PUBESURL);
        pssourcenumtrue = pssourcenumtrueService.get(2);
        pssourcenumtrue.setNum(ESParse.getTotal2(res));
        pssourcenumtrueService.update(pssourcenumtrue);
        pssourcenumtrueService.update(pssourcenumtrue);

        res = ESParse.execQuery(restClient, String.format(SOURCECOUNT, "paper"), PUBESURL);
        pssourcenumtrue = pssourcenumtrueService.get(3);
        pssourcenumtrue.setNum(ESParse.getTotal2(res));
        pssourcenumtrueService.update(pssourcenumtrue);

        res = ESParse.execQuery(restClient, String.format(SOURCECOUNT, "bbs"), PUBESURL);
        pssourcenumtrue = pssourcenumtrueService.get(4);
        pssourcenumtrue.setNum(ESParse.getTotal2(res));
        pssourcenumtrueService.update(pssourcenumtrue);

        res = ESParse.execQuery(restClient, String.format(SOURCECOUNT, "wb"), PUBESURL);
        pssourcenumtrue = pssourcenumtrueService.get(5);
        pssourcenumtrue.setNum(ESParse.getTotal2(res));
        pssourcenumtrueService.update(pssourcenumtrue);

        res = ESParse.execQuery(restClient, String.format(SOURCECOUNT, "wechat"), PUBESURL);
        pssourcenumtrue = pssourcenumtrueService.get(6);
        pssourcenumtrue.setNum(ESParse.getTotal2(res));
        pssourcenumtrueService.update(pssourcenumtrue);

        // 主题监测统计
        Integer num;
        PsitemmonitorDO psitemmonitor;
        num = ThirdJdbcTemplate.queryForObject("select count(*) from t_person", Integer.class);
         psitemmonitor = psitemmonitorService.get(2);
        psitemmonitor.setNum(num);
        psitemmonitorService.update(psitemmonitor);
        num = ThirdJdbcTemplate.queryForObject("select count(*) from t_special", Integer.class);
         psitemmonitor = psitemmonitorService.get(1);
        psitemmonitor.setNum(num);
        psitemmonitorService.update(psitemmonitor);
        num = ThirdJdbcTemplate.queryForObject("select count(*) from t_competitor", Integer.class);
         psitemmonitor = psitemmonitorService.get(3);
        psitemmonitor.setNum(num);
        psitemmonitorService.update(psitemmonitor);


        // 用户监控

        List<PDepart> departs = ThirdJdbcTemplate.query("select id,depart_name,area_name from t_depart", new BeanPropertyRowMapper<>(PDepart.class));
        for (PDepart depart:departs) {
            num =  ThirdJdbcTemplate.queryForObject("select count(*) from t_user where departid = ?",  new Object[]{depart.getId()},Integer.class);
            PsuserdataDO psuser = new PsuserdataDO();
            psuser.setId(depart.getId());
            psuser.setLevel2(depart.getArea_name());
            psuser.setLevel3(depart.getDepart_name());
            psuser.setNum(num);
            psuserdataService.saveUpdate(psuser);
        }

        // 采集源top10
        res = ESParse.execQuery(restClient, SOURCETOPTEN, PUBESURL);
        JSONArray arr = JSON.parseObject(res).getJSONObject("aggregations").getJSONObject("count").getJSONArray("buckets");
        for (int i=0 ;i<arr.size();i++) {
            PssitenumtureDO pssitetrue = new PssitenumtureDO();
            pssitetrue.setId(i+1);
            pssitetrue.setSite(arr.getJSONObject(i).getString("key"));
            pssitetrue.setNum(arr.getJSONObject(i).getLong("doc_count"));
            pssitenumtureService.saveUpdate(pssitetrue);
        }


    }






}
